package com.accp.controller;

import java.io.IOException;
import java.util.List;

import javax.jms.Session;
import javax.jws.WebParam.Mode;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.accp.pojo.UserInfo;
import com.accp.service.UserService;
import com.accp.service.impl.UserServiceImpl;

@Controller
public class UserController {

	@Autowired
	UserService userService = new UserServiceImpl();

	@RequestMapping("/tologin")
	public String tologin() {

		return "login";
	}

	@RequestMapping("/login")
	public String login(String loginid, String loginpwd,
			HttpServletRequest request, HttpServletResponse response) {

		UserInfo user = userService.getUser(loginid, loginpwd);

		HttpSession session = request.getSession();
		session.setAttribute("user", user);

		return "redirect:getAllGoods";
	}

	@RequestMapping("/tozhuce")
	public String tozhuce() {

		return "zhuce";
	}

	@RequestMapping("/zhuce")
	public String zhuce(String loginid,String loginpwd,String username) {

		
		UserInfo user=new UserInfo(loginid,0,loginpwd,username);
		
		
		int result=userService.add(user);
		
		if (result==1) {
			return "redirect:tologin";
		}else{
			
			return "zhuceshibai";
		}
		
		
	}

	
	
	
	
	
	@RequestMapping("/getUser")
	public String getUser(Model model){
		
		
		
		
		List<UserInfo> user=userService.getAllUser();
		
		
		model.addAttribute("userList", user);
		
		
		return "GMUser";
	}
	
	
	
	
	
	@RequestMapping("/toDeleteUser")
	public String deleteuser(String loginid){
		
		
		try {
			int result=userService.deleteByPrimaryKey(loginid);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return "redirect:getUser";
		
	}
	
	
	@RequestMapping("exitLogin")
	public String exitLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		
	
			
			HttpSession session =request.getSession();
			session.invalidate();//清除会话
			
			
		
		
		return "redirect:tologin";
	}
	
}
